/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ControleAcademico.Model.Pojo;

import java.io.Serializable;
import java.util.ArrayList;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.ManyToOne;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.OneToMany;

/**
 *
 * @author arthur
 * @author renan
 * @author tais
 * @author vinicius
 */

@Entity
@Table(name="Disciplina")
public class Disciplina implements Serializable{
    
    
    // Atributos
    @Id
    @GeneratedValue(strategy= GenerationType.IDENTITY)
    @Column(unique=true,nullable=false)
    private int id;
    
    @Column(name = "Nome",length = 45)
    private String nome;
    
    @Column(name = "Ementa",length = 45)
    private String ementa;
    
    @Column(name = "Carga_Horaria")
    private int cargaHoraria;
    
    @JoinColumn (name= "Turmas" ,referencedColumnName= "id")
    @OneToMany(mappedBy="disciplina")
    private ArrayList<Turma> turmas;
  
    @JoinColumn (name= "Historico_Disciplinas" ,referencedColumnName= "id")
    @OneToMany(mappedBy="disciplina")
    private ArrayList<HistoricoDisciplinas> historicoDisciplinas;
    
    @ManyToMany
    @JoinTable(name="Professor",
           joinColumns={
  	       @JoinColumn(name="id",
                           referencedColumnName="id")},
	   inverseJoinColumns={
               @JoinColumn(name="id",
                           referencedColumnName="id")})
    private ArrayList<Professor> professores;

    public Disciplina() {
    }
    
    // Metodos
    public String getNome(){return this.nome;}
    public void setNome(String nome){this.nome = nome;}
    public String getEmenta(){return this.ementa;}
    public void setEmenta(String ementa){this.ementa = ementa;}
    public int getCargaHoraria(){return this.cargaHoraria;}
    public void setCargaHoraria(int cargaHoraria){this.cargaHoraria = cargaHoraria;}
    public int getId(){return this.id;}
    public void setId(int id){this.id = id;}
    
    public void setTurmas(ArrayList<Turma> turmas) {
        this.turmas = turmas;
    }

    public void setHistoricoDisciplinas(ArrayList<HistoricoDisciplinas> historicoDisciplinas) {
        this.historicoDisciplinas = historicoDisciplinas;
    }

    public void setProfessores(ArrayList<Professor> professores) {
        this.professores = professores;
    }

    public ArrayList<Turma> getTurmas() {
        return turmas;
    }

    public ArrayList<HistoricoDisciplinas> getHistoricoDisciplinas() {
        return historicoDisciplinas;
    }

    public ArrayList<Professor> getProfessores() {
        return professores;
    }
    
    public Disciplina(String nome, String ementa, int  cargaHoraria, int id){
        this.nome = nome;
        this.ementa = ementa;
        this.cargaHoraria = cargaHoraria;
        this.id = id;
    }
    
    
    public HistoricoDisciplinas buscaAluno(Aluno aluno){
        
        int index = -1;
        for(HistoricoDisciplinas h : historicoDisciplinas) {
            if(h.getMatriculado().equals(aluno)) {
                index =  this.historicoDisciplinas.indexOf(h);
            }
        }
        
        HistoricoDisciplinas histDisciplinaEncontrado = this.historicoDisciplinas.get(index);
        return histDisciplinaEncontrado;
    }
    
    
    @Override
    public boolean equals(Object obj) {
        
        if(this == obj) {
            return true;
        }
        
        if(!(obj instanceof Disciplina)) {
            return false;
        }
         
        
	Disciplina c = (Disciplina) obj;
        String nome1 = c.getNome();
        String nome2 = this.getNome();
        
        String ementa1 = c.getEmenta();
        String ementa2 = this.getEmenta();
        
        int cargaHoraria1 = c.getCargaHoraria();
        int cargaHoraria2 = this.getCargaHoraria();
        
        if((nome1.equals(nome2) == true) && (ementa1.equals(ementa2) == true)
                && (cargaHoraria1 - cargaHoraria2 == 0)) {
            
            return true;
        } else {
            return false;
        }
    }
    
    
    @Override
    public String toString() {
        return("ID: " + this.getId() + "\t" + this.getNome() + "\t" + this.getEmenta()
                + "\t CH " + this.getCargaHoraria());
    }
    
}
